package com.shure.desensitize.serialize;

import com.zdww.unified.desensitize.annotation.IgnoreDesensitize;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.util.Objects;

/**
 * @ClassName UserInterceptor
 * @Description TODO
 * @Author shure
 * @Date 2023/9/22 12:52
 * @Version 1.0
 **/
public class DesensitizeInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        HandlerMethod method = (HandlerMethod) handler;
        IgnoreDesensitize ig = method.getMethodAnnotation(IgnoreDesensitize.class);
        if (Objects.nonNull(ig)) {
            DesensitizeTransfer.skipSensitive();
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        DesensitizeTransfer.removeValue();
    }
}
